/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.moxiu.browser;

import com.android.internal.preference.YesNoPreference;
import com.moxiu.browser.view.NightToast;
import com.moxiu.launcher.R;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

class BrowserYesNoPreference extends YesNoPreference {// This is the constructor called by the inflater
	private Context mContext = null;
    public BrowserYesNoPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        
       
        
    }
    private TextView exitText,confimText,titleText,contentText;
    private String key;

    @Override
	protected View onCreateDialogView() {  
    	
    	key=getKey();    	
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        View view = inflater.inflate(R.layout.br_dialog_exit, null);
        
        
        titleText=(TextView)view.findViewById(R.id.br_dialog_title_tv);
        contentText=(TextView)view.findViewById(R.id.br_dialog_word_tv);
        exitText=(TextView) view.findViewById(R.id.br_dialog_exit_cancel);
        exitText.setOnClickListener(new OnClickListener() {			
 			@Override
 			public void onClick(View arg0) {
 				Dialog dialog=getDialog();
 				if(dialog!=null) {
 					dialog.dismiss();
 				}
 			}
 		});  
        confimText=(TextView)view.findViewById(R.id.br_dialog_exit_exit);
        confimText.setText("确定");
        confimText.setOnClickListener(new listener());
    	if(key.equals("privacy_clear_history")){    		
    		titleText.setText("清除历史记录");   		
            contentText.setText("要删除历史记录吗？");
		  return view;
		}else if(key.equals("privacy_clear_passwords")) {
    		titleText.setText("清除密码");
            contentText.setText("要删除已保存的密码吗？");
    		return view;
		}else if(key.equals("privacy_clear_cache")) {
    		titleText.setText("清除缓存");
            contentText.setText("要删除本地缓存的内容和数据库吗？");
    		return view;
		}else if(key.equals("privacy_clear_cookies")) {
    		titleText.setText("清除Cookie");
            contentText.setText("要清除cookies吗?");
    		return view;
		}
		return view;
	}
    
    @Override
    	protected void showDialog(Bundle state) {
    		// TODO Auto-generated method stub
    		super.showDialog(state);
    		 getDialog().getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
    		 Window w = getDialog().getWindow();
    		 WindowManager.LayoutParams lp = w.getAttributes();
    		 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    		 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    		 
    		 w.setAttributes(lp);
    	}

    private class listener implements OnClickListener{
    BrowserSettings settings = BrowserSettings.getInstance();     
		@Override
		public void onClick(View arg0) {
			 Dialog dialog=getDialog();
			 if(key.equals("privacy_clear_history")){
					settings.clearHistory();
					if(dialog!=null)dialog.dismiss();
					Toast.makeText(mContext, R.string.br_clear_history_success, Toast.LENGTH_SHORT).show();
			 }else if(key.equals("privacy_clear_passwords")){
					settings.clearPasswords();
					if(dialog!=null)dialog.dismiss();
					Toast.makeText(mContext, R.string.br_clear_password_success, Toast.LENGTH_SHORT).show();
			 }else if(key.equals("privacy_clear_cache")){
					settings.clearCache();
               settings.clearDatabases();
               if(dialog!=null)dialog.dismiss();
               Toast.makeText(mContext, R.string.br_clear_cache_success, Toast.LENGTH_SHORT).show();
			 }else if(key.equals("privacy_clear_cookies")){
					settings.clearCookies();
					if(dialog!=null)dialog.dismiss();
		            Toast.makeText(mContext, R.string.br_clear_cookie_success, Toast.LENGTH_SHORT).show();
			 }
//			switch(key){
//			case "privacy_clear_history":
//				settings.clearHistory();
//				if(dialog!=null)dialog.dismiss();
//                Toast.makeText(mContext, R.string.br_clear_history_success, Toast.LENGTH_SHORT).show();
//                  break;
//			case "privacy_clear_passwords":
//				settings.clearPasswords();
//				if(dialog!=null)dialog.dismiss();
//                Toast.makeText(mContext, R.string.br_clear_password_success, Toast.LENGTH_SHORT).show();
//                  break;
//			case "privacy_clear_cache":
//				settings.clearCache();
//                settings.clearDatabases();
//                if(dialog!=null)dialog.dismiss();
//                Toast.makeText(mContext, R.string.br_clear_cache_success, Toast.LENGTH_SHORT).show();
//                  break;
//			case "privacy_clear_cookies":
//				settings.clearCookies();
//				if(dialog!=null)dialog.dismiss();
//	            Toast.makeText(mContext, R.string.br_clear_cookie_success, Toast.LENGTH_SHORT).show();
//	              break;
//			}
		}}
/*	@Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        
        if (positiveResult) {
            //setEnabled(false);//点击确认后不可以点击了
        	setEnabled(true);///点击确认后还可以点击
           
            if (PreferenceKeys.PREF_PRIVACY_CLEAR_CACHE.equals(getKey())) {
                settings.clearCache();
                settings.clearDatabases();
                Toast.makeText(mContext, R.string.br_clear_cache_success, Toast.LENGTH_SHORT).show();

            } else if (PreferenceKeys.PREF_PRIVACY_CLEAR_COOKIES.equals(getKey())) {
                settings.clearCookies();
                Toast.makeText(mContext, R.string.br_clear_cookie_success, Toast.LENGTH_SHORT).show();
 
            } else if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(getKey())) {
                settings.clearHistory();
                Toast.makeText(mContext, R.string.br_clear_history_success, Toast.LENGTH_SHORT).show();
 

            } else if (PreferenceKeys.PREF_PRIVACY_CLEAR_PASSWORDS.equals(getKey())) {
            	
                settings.clearPasswords();
                Toast.makeText(mContext, R.string.br_clear_password_success, Toast.LENGTH_SHORT).show();
            } 
        }
    }*/
	
}
